package com.putong35.common.utils.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.appbar.AppBarLayout;
import com.putong35.common.R;
import com.putong35.common.databinding.BaseHeaderContentScrollableIndependentBinding;

/**
 * Created by Donald Peng on 2018/4/6.
 * base_header_content_scrollable_independent.xml 的封装类
 * 提供简单方便的api供外部使用
 */

public class HeaderContentLayoutWraper {

    public BaseHeaderContentScrollableIndependentBinding headerContentBinding;
    public HeaderContentLayoutWraper(Context context, ViewGroup parentView) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        headerContentBinding = BaseHeaderContentScrollableIndependentBinding.inflate(inflater);
        parentView.addView(headerContentBinding.getRoot());
    }


    /*public void addHeaderView(ViewGroup headerView) {
        int childCount = headerView.getChildCount();
        List<View> childViews = new ArrayList<>();
        for (int i = 0; i < childCount; i++) {
            childViews.add(headerView.getChildAt(i));
        }
        for (int i = 0; i < childCount; i++) {
            headerView.removeView(childViews.get(i));
            headerContentBinding.lytHeadContent.addView(childViews.get(i));
        }
    }
*/
    public void addHeaderView(View headerView) {
        getAppBarLayout().addView(headerView);
    }

    /**
     *
     * @param headerView
     * @param flag  AppBarLayout.LayoutParams.SCROLL_FLAG_XXX  如果不设置，默认的flag是scroll
     */
    public void addHeaderView(View headerView, int flag) {
        AppBarLayout.LayoutParams lp = new AppBarLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setScrollFlags(flag);
        getAppBarLayout().addView(headerView, lp);
    }

    public void addContentView(FragmentManager fragmentManager, Fragment fragment) {
        fragmentManager.beginTransaction().add(R.id.lytList, fragment).commit();
        final SwipeRefreshLayout swipeLayout = getRefreshLayout();
        swipeLayout.setProgressViewOffset(true, -20, 100);
        swipeLayout.setColorSchemeResources(android.R.color.holo_purple, android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);

        getAppBarLayout().addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset >= 0) {
                    swipeLayout.setEnabled(true);
                } else {
                    swipeLayout.setEnabled(false);
                }
            }
        });
    }


    public void addContentView(View contentView) {
        headerContentBinding.lytList.addView(contentView);
        final SwipeRefreshLayout swipeLayout = getRefreshLayout();
        swipeLayout.setProgressViewOffset(true, -20, 100);
        swipeLayout.setColorSchemeResources(android.R.color.holo_purple, android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);

        getAppBarLayout().addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset >= 0) {
                    swipeLayout.setEnabled(true);
                } else {
                    swipeLayout.setEnabled(false);
                }
            }
        });
    }


    public SwipeRefreshLayout getRefreshLayout() {
        return headerContentBinding.slyt;
    }

    public AppBarLayout getAppBarLayout() {
        return headerContentBinding.appbarLayout;
    }


}
